package cn.raven.pet.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.basic.utils.LoginContext;
import cn.raven.org.domain.Employee;
import cn.raven.org.service.IEmployeeService;
import cn.raven.pet.domain.PetDetail;
import cn.raven.pet.service.IPetService;
import cn.raven.pet.domain.Pet;
import cn.raven.pet.query.PetQuery;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import cn.raven.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存pet",sn = "pet:save")
    public AjaxResult save(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Employee employee = LoginContext.getEmployee(request);
            pet.setShopId(employee.getShopId());
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.add(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除pet",sn = "pet:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            petService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "id查询pet",sn = "pet:id")
    public Pet findById(@PathVariable("id")Long id)
    {
        return petService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> findAll(){

        return petService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "pet列表",sn = "pet:list")
    public PageList<Pet> queryData(@RequestBody PetQuery query)
    {
        return petService.queryData(query);
    }

    /**
    * @Title: onsale
    * @Description: 宠物批量上架
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/16 16:40
    * @Parameters: [ids]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/onsale")
    @PreAuthorize(name = "宠物上架",sn = "pet:onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }

    /**
    * @Title: offsale
    * @Description: 宠物批量下架
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/16 16:41
    * @Parameters: [ids]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/offsale")
    @PreAuthorize(name = "宠物下架",sn = "pet:offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
    
    /** 
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/16 23:48
    * @Parameters: [ids]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PatchMapping("/batchDel")
    @PreAuthorize(name = "批量删除宠物",sn = "pet:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            petService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
    
    /** 
    * @Title: detail
    * @Description: 回显宠物详情
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/17 8:50  
    * @Parameters: [petId]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @GetMapping("/detail/{id}")
    @PreAuthorize(name = "回显宠物详情",sn = "pet:detail")
    public PetDetail detail(@PathVariable("id") Long id){
            return petService.findByPetId(id);
    }
}
